package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.AddDeptDto;
import com.itheima.admin.dto.DeptPageDto;
import com.itheima.admin.dto.UpdateDeptDto;
import com.itheima.admin.service.IDepartmentService;
import com.itheima.admin.vo.DeptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WXL
 */
@RestController
@RequestMapping("/dept")
@Api(value = "管理后台部门模块接口",tags = "admin_dept",description = "用于实现后台部门的增删改查操作")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @PostMapping("/list")
    @ApiOperation("分页查询部门列表")
    public PageVo<DeptVo> queryByPage(@RequestBody DeptPageDto deptPageDto){
        if (deptPageDto.getCurrentPage() == null){
            deptPageDto.setCurrentPage(1);
        }
        if (deptPageDto.getPageSize() == null){
            deptPageDto.setPageSize(5);
        }

        return departmentService.queryByPage(deptPageDto);

    }

    @GetMapping("/listall")
    @ApiOperation("查询所有部门信息")
    public List<DeptVo> queryAllDepartments(){

       return departmentService.queryAllDepartments();

    }

    @PostMapping("/add")
    @ApiOperation("新增部门")
    public Result addDepartment(@RequestBody AddDeptDto addDeptDto){

        boolean result = departmentService.addDepartment(addDeptDto);

        return new Result(result,null,result?"添加部门成功":"添加部门失败");

    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除部门")
    public Result deleteDepartment(@PathVariable("id") String deptId){
        boolean result = departmentService.deleteDepartment(deptId);

        return new Result(result,null,result?"删除部门成功":"删除部门失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询部门")
    public AddDeptDto queryById(@PathVariable("id") String deptId){

        return departmentService.queryById(deptId);

    }

    @PutMapping("/edit")
    @ApiOperation("修改部门")
    public Result editDepartment(@RequestBody UpdateDeptDto updateDeptDto){

        boolean result = departmentService.editDepartment(updateDeptDto);

        return new Result(result,null,result?"修改部门成功":"修改部门失败");
    }

}
